package com.fin.zw.intelligentbot.service;

import com.fin.zw.intelligentbot.entity.Company;
import com.fin.zw.intelligentbot.entity.QCompany;
import com.fin.zw.intelligentbot.model.request.CompanyRequest;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.repository.CompanyRepository;
import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service("companyService")
public class CompanyService {

    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private CommonService commonService;

    /**
     * 企业信息查询（分页）
     * @param pageable
     * @param predicate
     * @return
     */
    public Page<Company> queryCompanyList(Pageable pageable,Predicate predicate){
        Page<Company> page = companyRepository.findAll(predicate,pageable);
        return page;
    }

    /**
     * 获取企业信息（主要是供其他业务进行选择使用）
     * @param sessionModel
     * @return
     */
    public List<Company> getCompanyList(SessionModel sessionModel){
        List<Company> list = new ArrayList<>();
        if(sessionModel.getCompanyId() == null){
            list = companyRepository.findAll();
        }else{
            list.add(commonService.getCompany(sessionModel));
        }
        return list;
    }

    /**
     * 企业信息管理添加
     * @param request
     * @param sessionModel
     */
    public void createCompany(CompanyRequest request, SessionModel sessionModel) {
        if (StringUtils.isNotEmpty(request.getCode())) {
            Optional<Company> optional = companyRepository.findOne(QCompany.company.code.eq(request.getCode()));
            if (optional.isPresent()) {
                throw new RuntimeException("该企业编号已经存在");
            }
        }
        Company company = new Company();
        BeanUtils.copyProperties(request, company);
        company.setCreateName(sessionModel.getUserName());
        company.setCreateId(sessionModel.getUserId());
        company.setCreateTime(new Date());
        companyRepository.save(company);
    }

    /**
     * 企业信息更新
     * @param requset
     */
    public void updateCompany(CompanyRequest requset){
        Optional<Company> optional = companyRepository.findById(requset.getId());
        if(!optional.isPresent()){
            throw new RuntimeException("修改得企业信息不存在");
        }
        Company company = optional.get();
        company.setName(requset.getName());
        company.setAddress(requset.getAddress());
        company.setContactName(requset.getContactName());
        company.setContactPhone(requset.getContactPhone());
        company.setStatus(requset.getStatus());
        companyRepository.save(company);
    }


}
